package cn.com.ddapp.ddm.adapter;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by Administrator on 2016/8/17 0017.
 */
public class HomePagerAdapter extends PagerAdapter {
    private List<ImageView> ivs;


    public HomePagerAdapter(List<ImageView> ivs) {
        super();
        this.ivs = ivs;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
//        ImageView imageView = this.ivs.get(position % ivs.size());
//        if (imageView.getParent() != null) {
//            ((ViewPager) imageView.getParent()).removeView(imageView);
//
//        }
//        ((ViewPager) container).addView((View) imageView, 0);

        View view=this.ivs.get(position %ivs.size());
        ViewParent vp =view.getParent();
        if (vp!=null){
            ViewGroup parent = (ViewGroup)vp;
            parent.removeView(view);
        }
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        View view = this.ivs.get(position % ivs.size());
//        container.removeView(view);
    }
}
